package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            //当spring security 完成认证后，会将当前用户信息保存到框架提供的上下文对象
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
    }
    //设置修改权限
    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("/findPage")
    //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用分页查询方法
        PageResult pageResult = userService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        //返回PageResult
        return pageResult;
    }

    //设置权限
    @PreAuthorize("hasAnyAuthority('USER_ADD')")
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        //try
        try {
            //调用add方法
            userService.add(user, roleIds);
            //返回添加成功
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回添加失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    //添加编辑权限
    @PreAuthorize("hasAnyAuthority('USER_EDIT')")
    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        //try
        try {
            //调用编辑方法
            userService.edit(user, roleIds);
            //返回编辑成功
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    //设置修改权限
    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("/findById")
    //通过id查询
    public Result findById(Integer id) {
        //调用id查询方法
        com.itheima.pojo.User user = userService.findById(id);
        //判断数据是否为空
        if (user != null) {
            //数据不为空新建查询成功Result
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            //result调用setData方法吧查询结果添加进result
            result.setData(user);
            //返回result
            return result;
        } else {
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    @PreAuthorize("hasAnyAuthority('USER_DELETE')")
    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用删除方法
            userService.delete(id);
        } catch (RuntimeException e) {
            //catch自定义错误并返回
            return new Result(false,MessageConstant.DELETE_USER_FAIL );
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        //返回删除成功
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }

    //设置修改权限
    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("/findRolesByUserId")
    //通过检查组id查询检查项
    public Result findRolesByUserId(Integer id) {
        //try
        try {
            //调用查询方法返回检查项id集合
            List<Integer> roleIds = userService.findRolesByUserId(id);
            //返回查询成功带检查项id集合
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
